package de.westranger.forex.trading.preprocessing;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

public class CSVFileSplitter {

	private static final int numLinesPerFile = 20000;

	public static void main(String[] args) throws IOException {
		//final File file = new File("E:\\EUR_USD_2012\\indicator_wk1\\mql_validation_java_indicator.csv");
		final File file = new File("F:\\Program Files (x86)\\OANDA - MetaTrader\\tester\\files\\ema_train.csv");
		final BufferedReader br = new BufferedReader(new FileReader(file));
		final String firstLine = br.readLine()+'\n';

		BufferedOutputStream bw = null;
		int lineCounter = 0;
		int fileCounter = 0;

		while(br.ready()){
			if(lineCounter % numLinesPerFile == 0){
				if(bw != null){
					bw.close();
				}
				final String path = file.getParent()+"\\"+file.getName().replace(".csv", "")+"_"+Integer.toString(fileCounter++)+".csv.gz";
				bw = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(path)));
				bw.write(firstLine.getBytes());
			}

			bw.write(br.readLine().getBytes());
			bw.write('\n');
			lineCounter++;
		}

		bw.close();
		br.close();
	}

}
